<template>
  <div class="app-container user-form-container">

  </div>
</template>

<script>
import { getUserToken, setUserToken } from '@/utils/auth'
import infoApi  from '@/api/info';
import weixinApi  from '@/api/weixin';
import store from '@/store'

export default {
  name: 'Auth',
  components: {},
  data() {
    return {
      qrcode: '',
      wx_oauth_code: '',
      filled: 0,
      bindContent: '',
    }
  },

  created() {
    if (this.$route.query.qrcode) {
      console.log("created qrcode:",this.$route.query.qrcode);
      this.qrcode = this.$route.query.qrcode
    }
    if(this.$route.query.code){
      console.log("created wx auth code:",this.$route.query.code);
      this.wx_oauth_code = this.$route.query.code;
    }

    if(this.wx_oauth_code){
      console.log("created wx_oauth_code:",this.wx_oauth_code);

      //获取用户信息及二维码信息
      weixinApi.getAuthInfo(this.wx_oauth_code,this.$route.query.state).then(res => {
        console.log("getAuthInfo:",res);

        if (res.code == 0 && res.data) {
          this.filled = res.data.filled;
          this.bindContent = res.data.bindContent;

          if(res.data.token){
            console.log("auth 登录成功 ：",res.data.token);
            console.log("before SET_USER_TOKEN:",store.getters.userToken);
            store.dispatch('user/setUserToken', res.data.token);
            console.log("after SET_USER_TOKEN:",store.getters.userToken);
            setUserToken(res.data.token);
          }

          if(this.filled && this.bindContent && this.bindContent.indexOf("http")==0){
            window.location.href = res.data.bindContent;
            return;
          }
          let query = {};
          if(res.data.bindContent){
            query.redirect = encodeURIComponent(res.data.bindContent);
          }
          if(res.data.communityId){
            query.community_id = res.data.communityId;
          }

          this.$router.push({"path":"/ui/info_form","query":query});
        }else{
          console.log("getAuthInfo error:",res);
          //提示错误
        }
      }).catch(()=>{
        console.log("getAuthInfo catch error:");
      });
    }else {
      let token = getUserToken();
      console.log("get token : ",token);
      if(!token){
        console.log("has no token ， redirect to oauth ");
        window.location.href = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx9a55447c32494ebf&redirect_uri=http%3A%2F%2Fims.zhenghangqirui.cn%2Fui%2Fauth&response_type=code&scope=snsapi_base&state="+this.qrcode+"&connect_redirect=1#wechat_redirect";
        return;
      }

      infoApi.getQrcodeInfo(this.qrcode).then(res => {
        console.log("request  getQrcodeInfo res:",res);

        if (res.code == 0 && res.data) {
          this.filled = res.data.filled;
          this.bindContent = res.data.bindContent;

          if(this.filled && this.bindContent && this.bindContent.indexOf("http")==0){
            window.location.href = res.data.bindContent;
            return;
          }

          let query = {};
          if(res.data.bindContent){
            query.redirect = encodeURIComponent(res.data.bindContent);
          }
          if(res.data.communityId){
            query.community_id = res.data.communityId;
          }
          this.$router.push({"path":"/ui/info_form","query":query});
        }else{
          console.log("request  getQrcodeInfo error:",res);
          this.$dialog.alert({
            message: res.msg?res.msg:"系统错误",
          });
          //提示错误
          return;
        }
      }).catch(() => {
        console.log('catch error ')
        this.$dialog.alert({message: "网络错误~"});
      })

    }
  },
  methods: {

  }
}
</script>
